package startup.security.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import startup.security.service.FileService;
import startup.security.service.S3FileManagerService;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ly
 * @date : 2023/02/22
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/user")
@Api(tags = "亚马逊云文件上传")
public class SysAwsFileController {

    @Autowired
    FileService fileService;
    @Resource
    private S3FileManagerService s3FileManagerService;

    @ApiOperation("普通的文件上传无访问角色权限控制")
    @PostMapping("/list")
    public String getSysUserList(@RequestParam("file") MultipartFile file) throws IOException {

// 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码
            File file2 = File.createTempFile(fileName, prefix);
            file.transferTo(file2);
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        String filename= fileService.upload("/",toFile);
       return filename;
    }


    @ApiOperation(value = "上传文件接口")
    @PostMapping(value = "/upload")
    public void upload(MultipartFile file) {
        s3FileManagerService.upload(file);
    }

    @ApiOperation(value = "上传文件接口")
    @PostMapping(value = "/down")
    public void download(String arg1,String arg2) {
        s3FileManagerService.download("epc-dev","pic/20230304/1677860476379login.png");
    }



    private void inputStreamToFile(InputStream ins, File toFile) {
    }

}
